package com.dg.service;

import java.util.List;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import com.dg.core.pojo.ParamsData;
import com.dg.dao.DAOImpl;
import com.dg.pojo.Person;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Service("personService")
public class PersonService {

    @Resource(name = "dao")
    private DAOImpl dao;

    //只查person
    public List<Person> findAll() {
        return (List<Person>) dao.findForList("PersonMapper.getPersonList", null);
    }

    //关联查询
    public JSONObject findAllPD(String pageNumber, String pageSize) {
        int Number = new Integer(pageNumber);
        int Size = new Integer(pageSize);

        PageHelper.startPage(Number, Size);    //分页
        List<ParamsData> list = (List<ParamsData>) dao.findForList("PersonMapper.getPersonAndDeptList", null);
        //获取分页信息
        PageInfo<ParamsData> pageInfo = new PageInfo<ParamsData>(list);
        long total = pageInfo.getTotal(); //获取总记录数

        //封装成bootstrap-table格式
        JSONObject json = new JSONObject();
        json.put("total", total);
        json.put("rows", list);

        System.out.println(json);
        return json;
    }

    //新增
    public JSONObject addPerson(Person person) {
        int result = (int) dao.save("PersonMapper.addPerson", person);
        JSONObject json = new JSONObject();
        if (result == 1) {
            json.put("msg", "success");
        } else {
            json.put("msg", "error");
        }
        json.put("code", "200");

        return json;
    }

}
